package controller

import (
	"ez/apps/erp/auto/controller"
	erp "ez/apps/erp/document"
	"ez/custom/cc"
	"gitee.com/dreamwood/ez-go/ez"
)

type SellFeedbackController struct {
	controller.SellFeedbackAutoController
}

func init() {
	c := &SellFeedbackController{}
	c.SetRouteParam("/erp", "/admin", "SellFeedback")
	ez.CreateApi(c.AdminGet(), c.GetAction).SetRouterName("[erp]销售报价_创建")
	ez.CreateApi(c.AdminList(), c.ListAction).SetRouterName("[erp]销售报价_列表")
	ez.CreateApi(c.AdminSave(), c.SaveAction).SetRouterName("[erp]销售报价_保存")
	ez.CreateApi(c.AdminCopy(), c.CopyAction).SetRouterName("[erp]销售报价_复制")
	ez.CreateApi(c.AdminUpdate(), c.UpdateAction).SetRouterName("[erp]销售报价_更新")
	ez.CreateApi(c.AdminChoice(), c.ChoiceAction).SetRouterName("[erp]销售报价_选项")
	ez.CreateApi(c.AdminDelete(), c.DeleteAction).SetRouterName("[erp]销售报价_删除")
	ez.CreateApi(c.AdminUnDelete(), c.UnDeleteAction).SetRouterName("[erp]销售报价_恢复")
	ez.CreateApi(c.AdminDestroy(), c.DestroyAction).SetRouterName("[erp]销售报价_销毁")
	ez.CreateApi(c.AdminUpdateMany(), c.UpdateManyAction).SetRouterName("[erp]销售报价_更新_批量")
	ez.CreateApi(c.AdminDeleteMany(), c.DeleteManyAction).SetRouterName("[erp]销售报价_删除_批量")
	ez.CreateApi(c.AdminDestroyMany(), c.DestroyManyAction).SetRouterName("[erp]销售报价_销毁_批量")
	ez.CreateApi(c.AdminApi("/SellFeedback/fast_sellOrder"), c.FastSellOrderAction).SetRouterName("[erp]销售报价_销毁_批量")
}

//func (c SellFeedbackController) AccessControl(session *ez.Session) { /* 在这里面重构 */ }
//func (c SellFeedbackController) GetAction(session *ez.Session) { /* 在这里面重构 */ }
//func (c SellFeedbackController) ListAction(session *ez.Session) { /* 在这里面重构 */ }

func (c SellFeedbackController) FastSellOrderAction(session *ez.Session) {
	this := cc.New(session)

	sellFeedback, e := erp.NewSellFeedbackCrud(session).FindId(this.Get("id").IsInt64())
	if this.Try(e) {
		return
	}
	sellFeedback.LoadProduct()

	//创建询价单
	sellOrder := new(erp.SellOrder)
	sellOrder.Sn = ""
	sellOrder.Title = sellFeedback.Title + " #快速销售订单"
	sellOrder.GetFactory().SetArgus(session)
	sellOrder.SetEvent(true)
	if this.Try(sellOrder.Save()) {
		return
	}
	for _, product := range sellFeedback.Product {
		product.GetFactory().SetArgus(session)
		sellOrderProduct := new(erp.SellOrderProduct)
		sellOrderProduct.UnSerialize(product.Serialize())
		sellOrderProduct.BelongToId = sellOrder.Id
		sellOrderProduct.Src = sellFeedback.Id
		sellOrderProduct.SrcSN = sellFeedback.Sn
		sellOrderProduct.Id = 0
		sellOrderProduct.GetFactory().SetArgus(session)
		if this.Try(sellOrderProduct.Save()) {
			return
		}
	}
	this.ReturnSuccess("快速创建成功", "")
}
